#include "stm32f10x.h"
#include <stdio.h>

void uart_com1_init(void)
{

	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStrue;

	/* USART1 clock set*/
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
	
	/*USART1 TX, 推挽输出*/
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	/* RX 浮空输入*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	/*配置USART1模式*/
	USART_InitStructure.USART_BaudRate = 9600;                               //波特率设置为9600//
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;               //配置串口传输的字长8位//
	USART_InitStructure.USART_StopBits = USART_StopBits_1;                    //配置停止位为1//
	USART_InitStructure.USART_Parity = USART_Parity_No;                       //不设置奇偶校验位//
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //不采用硬件流//
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;						//配置串口为双线全双工通信，将RX、Tx模式都开启//
	USART_Init(USART1, &USART_InitStructure);																	//向寄存器写入配置参数//

	
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);                    //
  //USART_ITConfig(USART1, USART_IT_TXE, ENABLE); 
	
	USART_Cmd(USART1, ENABLE);
	/*
	NVIC_InitStrue.NVIC_IRQChannel=USART1_IRQn;
	NVIC_InitStrue.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority=1;
	NVIC_InitStrue.NVIC_IRQChannelSubPriority=1;
	NVIC_Init(&NVIC_InitStrue);*/
}
void uart_com1_deinit(void)
{
	GPIO_DeInit(GPIOA);
	USART_DeInit(USART1);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,DISABLE);
}
void uart_com2_init(void)
{

	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStrue;

	/* USART2 clock set*/
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
	/*USART2 TX, 推挽输出*/
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	/* RX 浮空输入*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
			/*485 CTRL DX, 推挽输出*/
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	
	/*配置USART2模式*/
	USART_InitStructure.USART_BaudRate = 9600;                               //波特率设置为9600//
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;               //配置串口传输的字长8位//
	USART_InitStructure.USART_StopBits = USART_StopBits_1;                    //配置停止位为1//
	USART_InitStructure.USART_Parity = USART_Parity_No;                       //不设置奇偶校验位//
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //不采用硬件流//
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;						//配置串口为双线全双工通信，将RX、Tx模式都开启//
	
	USART_Init(USART2, &USART_InitStructure);																	//向寄存器写入配置参数//
	
	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);                    //
  //USART_ITConfig(USART1, USART_IT_TXE, ENABLE); 
	
	USART_Cmd(USART2, ENABLE);
}

void uart_com2_deinit(void)
{
	GPIO_DeInit(GPIOA);
	USART_DeInit(USART2);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,DISABLE);
}
void uart_send_data(USART_TypeDef* USARTx, uint16_t Data)
{
    USART_SendData(USARTx, Data);
		while(USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET){}
}



